/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.QuyDinh;
import util.HibernateUtil;

/**
 *
 * @author Phi
 */
public class QuiDinhDAO {
     public static List<QuyDinh> layDanhSachQuyDinh() throws HibernateException {
        List<QuyDinh> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "select quydinh from QuyDinh quydinh";
        Query query = session.createQuery(hql);
        ds = query.list();
        session.close();
        return ds;
    }
 
    public static QuyDinh layThongTinQuyDinh(int maQuyDinh) {
        QuyDinh qd = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            qd = (QuyDinh) session.get(QuyDinh.class, maQuyDinh);
           // Hibernate.initialize(qd.getCuaHang());
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
        }
        return qd;
    }
   
    public static boolean themThongTinQuyDinh(QuyDinh qd) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (QuiDinhDAO.layThongTinQuyDinh(qd.getMaQuyDinh()) != null) {
            return false;
        }
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.save(qd);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }
 
    public static boolean xoaQuyDinh(int maQuyDinh) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        QuyDinh qd = QuiDinhDAO.layThongTinQuyDinh(maQuyDinh);
        if (qd == null) {
            return false;
        }
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.delete(qd);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }
    
    public static boolean capNhatThongTinQuyDinh(QuyDinh qd) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        if (QuiDinhDAO.layThongTinQuyDinh(qd.getMaQuyDinh()) != null) {
            return false;
        }
        boolean kq = true;
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            session.update(qd);
            transaction.commit();
        } catch (HibernateException ex) {
            //Log the exception
            transaction.rollback();
            System.err.println(ex);
            kq = false;
        } finally {
            session.close();
        }
        return kq;
    }
    
   public static List<QuyDinh> timKiemQuyDinh(String dieuKienThu,int maCuaHang) {
        List<QuyDinh> ds;
        ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try { 
            String hql="select qd from QuyDinh qd where qd.dieuKienThu like '%"
                    + dieuKienThu+"%' and qd.cuaHang.maCuaHang ="
                    + maCuaHang;
            Query query = session.createQuery(hql); 
            ds = query.list(); 
        } catch (HibernateException ex) { 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return ds;
    }
   public static QuyDinh layAllQuyDinh(int maCuaHang) {
        QuyDinh qd = null; 
        Session session = HibernateUtil.getSessionFactory().openSession();
        try { 
            String hql = "select qd from QuyDinh qd where qd.cuaHang.maCuaHang =:maCuaHang";
            org.hibernate.Query query = session.createQuery(hql); 
            query.setInteger("maCuaHang", maCuaHang);
            qd = (QuyDinh)query.uniqueResult(); 
        } catch (HibernateException ex) { 
            //Log the exception 
            System.err.println(ex); 
        } finally { 
            session.close(); 
        } 
        return qd;
    }
}
